<template>
  <div class="musicScore" v-title :data-title="$t('router.scoreTitle')">
    <div class="search-box">
      <input
        v-model="keyword"
        @keyup.enter="search"
        type="text"
        :placeholder="$t('musicScore.search')"
        class="search"
      />
      <i class="el-icon-search search-icon" @click="search"></i>
    </div>
    <div
      class="list"
      v-infinite-scroll="load"
      infinite-scroll-disabled="isPage"
      infinite-scroll-delay="120"
      infinite-scroll-distance="10"
      infinite-scroll-immediate="false"
    >
      <album-list
        :listData="list"
        :diff="'musicScore-box'"
        :num="2"
        :loading="loading"
        v-loading="fullscreenLoading"
      ></album-list>
    </div>
    <nice-empty
      v-show="list.length == 0 && fullscreenLoading == false"
      :emptyText="$t('public.noData')"
    ></nice-empty>
    <div class="page-wrap" v-show="isPage">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-size="params.pageSize"
        background
        hide-on-single-page
        layout="total, prev, pager, next, jumper"
        :total="total"
        :small="isMobile"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import albumList from "@/components/common/albumList/Index";
export default {
  data() {
    return {
      keyword: "", //搜索关键词
      list: [],
      // 是否显示加载动画
      loading: false,
      // 是否还有数据
      loadStatus: true,
      // 请求参数
      params: {
        // 返回数量
        pageSize: 30,
        // 当前页数
        pageNum: 1,
      },
      //数据载入动画
      fullscreenLoading: true,
      //是否显示分页 / 加载更多
      isPage: false,
      total: 0, //总条数
    };
  },
  computed: {
    isMobile() {
      return this.$store.getters.isMobile;
    },
    isIE() {
      return this.$store.getters.isIE;
    },
  },
  components: {
    albumList,
  },
  mounted() {
    if (this.isIE) {
      this.params.pageSize = 50;
    }
    this.getScores();
  },
  methods: {
    search() {
      if (this.$i18n.locale == "zh") {
        this.params.cname = this.keyword;
      } else {
        this.params.name = this.keyword;
      }
      this.params.pageNum = 1;
      // this.list = [];
      this.getScores();
    },
    //分页 每页条数
    handleSizeChange(val) {
      this.params.pageSize = val;
      // this.offset = this.limit * this.currentPage
      this.getScores();
    },
    //当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      // this.list = [];
      // this.params.pageSize = (val - 1) * this.limit
      this.getScores();
    },
    // 获取乐谱列表
    async getScores() {
      this.fullscreenLoading = true;
      try {
        this.loadStatus = false;
        let res = await this.$api.score.getScores(this.params);
        if (res.code === 1000000) {
          // this.list = this.utils.processingData(this.list, res.source);
          this.list = res.source ? res.source : [];
          this.list.forEach((item) => {
            item.cover500 = `http://image.kuke.com/images/audio/musicbook/cover/${item.file.substr(
              0,
              1
            )}/${item.file}.jpg`;
            item.scoreView = `http://image.kuke.com/images/audio/musicbook/preview/${item.file.substr(
              0,
              1
            )}/${item.file}.jpg`;
          });
          this.total = res.total;
          this.fullscreenLoading = false;
          if (res.size < 50 && res.size < res.total) {
            this.loading = true;
            this.loadStatus = true;
            this.isPage = false;
          } else {
            this.loading = false;
            this.isPage = true;
          }
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 加载更多
    load() {
      this.params.pageSize += 20;
      this.getScores();
    },
  },
};
</script>

<style lang="scss" scoped>
.musicScore {
  .search-box {
    width: 480px;
    height: 40px;
    margin: auto;
    padding-left: 16px;
    padding-right: 40px;
    border-radius: 2px;
    background: #f8f8f8;
    position: relative;
    input {
      width: 100%;
      height: 40px;
      line-height: 40px;
      border: none;
      background: #f8f8f8;
      outline: none;
    }
    .search-icon {
      position: absolute;
      top: 10px;
      font-size: 20px;
      cursor: pointer;
    }
  }
}
</style>
